package com.heartrate.module.heart;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.heartrate.AndroidApplication;
import com.heartrate.R;
import com.heartrate.greendao.dao.DaoSession;
import com.heartrate.greendao.dao.HeartRatesDao;
import com.heartrate.local.table.HeartRates;
import com.heartrate.module.base.BaseActivity;
import com.heartrate.module.heart.adapter.ListAdapter;
import com.heartrate.tool.RingView;
import com.heartrate.tool.graphs.CallbackRingView;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/8/9.
 */

public class HeartRateAnimation extends BaseActivity implements CallbackRingView{

    @BindView(R.id.ringView)
    RingView mringView;//计心率动画
    @BindView(R.id.heart_rate_text)
    TextView heartRateValue;//心率值
    @BindView(R.id.heart_rate_unit)
    TextView heartRateUnit;//心率单位
    @BindView(R.id.list_view)
    ListView listView;
    @BindView(R.id.startHeartBeatTest)
    Button startHeartTest;

    private DaoSession daoSession;
    private List<HeartRates> listHeart;
    private HeartRatesDao heartRateEntityDao;
    ListAdapter listAdapter;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            heartRateValue.setVisibility(View.VISIBLE);
            heartRateUnit.setVisibility(View.VISIBLE);
            listAdapter.notifyDataSetChanged();
            startHeartTest.setEnabled(true);
            startHeartTest.setText("点击再次测心率");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_animation);
        ButterKnife.bind(this);
        heartRateValue.setVisibility(View.INVISIBLE);
        heartRateUnit.setVisibility(View.INVISIBLE);

        daoSession = ((AndroidApplication)getApplication()).getDaoSession();
        heartRateEntityDao = daoSession.getHeartRatesDao();
        listHeart = heartRateEntityDao.queryBuilder().list();
        Collections.reverse(listHeart);//对listHeart进行反转
        listAdapter = new ListAdapter(HeartRateAnimation.this,R.layout.list_item,listHeart);
        listView.setAdapter(listAdapter);

        startHeartTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                heartRateValue.setVisibility(View.INVISIBLE);
                heartRateUnit.setVisibility(View.INVISIBLE);
                mringView.startAnim(HeartRateAnimation.this);
                startHeartTest.setEnabled(false);
            }
        });

    }
    /**
     *
     * @param finish
     */
    @Override
    public void success(String finish) {
        if (finish.equals("success")){
            HeartRates heartRates = new HeartRates(null,66,77);
            heartRateEntityDao.insert(heartRates);
            listHeart.clear();
            listHeart.addAll(heartRateEntityDao.queryBuilder().list());
            Collections.reverse(listHeart);
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    }
}
